package com.example.new6.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.example.new6.R;
import com.example.new6.entity.Hot;

import java.util.List;

public class HotAdapter extends ArrayAdapter<Hot> {
    public HotAdapter(Context context, List<Hot> items) {
        super(context, 0, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 检查是否有现有的View可以重用，否则就创建一个新的
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_hot, parent, false);
        }

        // 获取数据项
        Hot item = getItem(position);

        // 查找视图并设置数据
        TextView titleView = convertView.findViewById(R.id.title);
        TextView indexView = convertView.findViewById(R.id.index);
        TextView hotValueView = convertView.findViewById(R.id.hotValue);
        LinearLayout linkLayout = convertView.findViewById(R.id.link);

        titleView.setText(item.getTitle());
        indexView.setText(String.valueOf(item.getIndex()));
        hotValueView.setText(item.getHotValue());

        View finalConvertView = convertView;

        linkLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url =item.getLink();
                if (url != null){
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    finalConvertView.getContext().startActivity(intent);
                }
            }
        });

        return convertView;
    }
}
